//
//  ActivityViewController1.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ActivityViewController1.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "UIImageView+WebCache.h"
#import "ActivityModel.h"
#import "BYJDataBaseHandle.h"

@interface ActivityViewController1 ()

@property (nonatomic, retain) UIScrollView *scrol;

@end

@implementation ActivityViewController1

- (void)dealloc {
    [_scrol release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor magentaColor];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = _DIC[@"title"];
    
    
    [self createll];
}

// 开始铺视图
- (void)createll {
    
    // 创建滚动视图
    self.scrol = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    _scrol.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scrol];
    [_scrol release];
    
    NSDictionary *att = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [_DIC[@"content"] boundingRectWithSize:CGSizeMake(340, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:att context:nil];
    _scrol.contentSize = CGSizeMake(0, rect.size.height + 300);

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 50)];
    titleLabel.backgroundColor = [UIColor clearColor];
    [_scrol addSubview:titleLabel];
    titleLabel.text = _DIC[@"title"];
    titleLabel.font = [UIFont systemFontOfSize:25];
    [titleLabel release];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 110, 150)];
    imageView1.backgroundColor = [UIColor blackColor];
    [_scrol addSubview:imageView1];
    NSString *imgStr = _DIC[@"image"];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:imgStr]];
    [imageView1 release];
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 90, 16, 16)];
    imageView2.backgroundColor = [UIColor clearColor];
    imageView2.image = [UIImage imageNamed:@"icon_date_blue@2x.png"];
    [_scrol addSubview:imageView2];
    [imageView2 release];
    
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 120, 16, 16)];
    imageView3.backgroundColor = [UIColor clearColor];
    imageView3.image = [UIImage imageNamed:@"icon_sponsor_blue.png"];
    [_scrol addSubview:imageView3];
    [imageView3 release];
    
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 150, 16, 16)];
    imageView4.backgroundColor = [UIColor clearColor];
    imageView4.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    [_scrol addSubview:imageView4];
    [imageView4 release];
    
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(140, 180, 16, 16)];
    imageView5.backgroundColor = [UIColor clearColor];
    imageView5.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    [_scrol addSubview:imageView5];
    [imageView5 release];
    
    UILabel *dataLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 85, 200, 30)];
    dataLabel.backgroundColor = [UIColor clearColor];
    NSString *str1 = [NSString stringWithFormat:@"%@", _DIC[@"begin_time"]];
    NSString *str2 = [str1 substringWithRange:NSMakeRange(5, 11)];
    NSString *str3 = [NSString stringWithFormat:@"%@", _DIC[@"end_time"]];
    NSString *str4 = [str3 substringWithRange:NSMakeRange(5, 11)];
    NSString *str = [NSString stringWithFormat:@"%@|%@", str2, str4];
    dataLabel.text =  str;
    [_scrol addSubview:dataLabel];
    [dataLabel release];
    
    UILabel *musLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 115, 200, 30)];
    musLabel.backgroundColor = [UIColor clearColor];
    musLabel.text = _DIC[@"owner"][@"name"];
    [_scrol addSubview:musLabel];
    [musLabel release];
    
    UILabel *typLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 145, 50, 30)];
    typLabel.backgroundColor = [UIColor clearColor];
    [_scrol addSubview:typLabel];
    typLabel.text = @"类型:";
    [typLabel release];
    
    UILabel *typLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(220, 145, 100, 30)];
    typLabel1.backgroundColor = [UIColor clearColor];
    typLabel1.text = _DIC[@"category_name"];
    [_scrol addSubview:typLabel1];
    [typLabel1 release];
    
    UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 175, 200, 50)];
    addLabel.backgroundColor = [UIColor clearColor];
    addLabel.text = _DIC[@"address"];
    addLabel.numberOfLines = 2;
    [_scrol addSubview:addLabel];
    [addLabel release];
    
    UILabel *actLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 300, 50)];
    actLabel.backgroundColor = [UIColor clearColor];
    actLabel.text = @"活动介绍";
    actLabel.font = [UIFont systemFontOfSize:25];
    [_scrol addSubview:actLabel];
    [actLabel release];
    
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor clearColor];
    [_scrol addSubview:label];
    label.numberOfLines = 100;
    label.text = _DIC[@"content"];
    label.frame = CGRectMake(20, 300, 340, rect.size.height);
    label.font = [UIFont systemFontOfSize:17];
    [label release];
    
    
    // 创建左边按钮
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(itemAction:)];
    self.navigationItem.leftBarButtonItem = item1;
    
    // 创建右侧按钮
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStyleDone target:self action:@selector(itemAction1:)];
    self.navigationItem.rightBarButtonItem = item2;
}


// 左边按钮的方法
- (void)itemAction:(UIBarButtonItem *)item {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

// 右侧按钮的方法
- (void)itemAction1:(UIBarButtonItem *)item {
    
    // 查询数据库
    NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectActivityTable];
    
    NSMutableArray *arrTitle = [NSMutableArray array];
    // 把数据库中的title取出来
    for (ActivityModel *model in arr) {
        NSString *strTitle = model.title;
        [arrTitle addObject:strTitle];
    }
    
    // 判断数组中是否存在title
    if (![arrTitle containsObject:self.navigationItem.title]) {
        
        ActivityModel *model = [ActivityModel activityModel];
        [model setValuesForKeysWithDictionary:_DIC];
        
        // 添加到数据库中
        [[BYJDataBaseHandle sharedDataBaseHandle] insertActivityModel:model];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
